All files / web/src/app/api/admin/blog/[slug]/refine-prompt route.ts

0% Statements 0/65
0% Branches 0/1
0% Functions 0/1
0% Lines 0/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                                                                                                   
import fs from 'fs'
import path from 'path'
import matter from 'gray-matter'
import { NextResponse } from 'next/server'
import { z } from 'zod'
import { llm } from '@/lib/llm'
import { withAuth } from '@/lib/auth/withAuth'
import { trackedCall } from '@/lib/ai-usage/llm-middleware'
import { AiFeature } from '@/lib/ai-usage/features'

const postsDirectory = path.join(process.cwd(), 'content', 'blog')

const SYSTEM_CONTEXT = `You are an expert prompt engineer for AI image generation. You're writing prompts for Abaci.one — an educational platform teaching mental math through the soroban (Japanese abacus). The image will be a hero/banner on the blog listing page displayed at 2.4:1 aspect ratio. Improve the given prompt to be more vivid, specific, and effective for image generation, without changing the subject or adding concepts not already indicated.`

/**
 * POST /api/admin/blog/[slug]/refine-prompt
 *
 * Uses an LLM to refine the current heroPrompt for better image generation.
 */
export const POST = withAuth(
  async (_request, { userId, params }) => {
    const { slug } = (await params) as { slug: string }

    const filePath = path.join(postsDirectory, `${slug}.md`)
    if (!fs.existsSync(filePath)) {
      return NextResponse.json({ error: 'Post not found' }, { status: 404 })
    }

    const fileContents = fs.readFileSync(filePath, 'utf8')
    const { data } = matter(fileContents)

    const heroPrompt = data.heroPrompt as string | undefined
    if (!heroPrompt) {
      return NextResponse.json({ error: 'Post has no heroPrompt' }, { status: 400 })
    }

    const title = (data.title as string) || 'Untitled'
    const description = (data.description as string) || ''

    const response = await trackedCall(
      llm,
      {
        prompt: `${SYSTEM_CONTEXT}

Blog post title: "${title}"
Blog post description: "${description}"

Current hero image prompt:
"${heroPrompt}"

Provide an improved version of this prompt.`,
        schema: z.object({
          refined: z.string().describe('The improved image generation prompt'),
        }),
      },
      { userId, feature: AiFeature.BLOG_REFINE_PROMPT }
    )

    return NextResponse.json({
      original: heroPrompt,
      refined: response.data.refined,
    })
  },
  { role: 'admin' }
)